#include <iostream>
#include <memory>
#include <string>
#include <cstring>
#include <cstdlib>
#include "TcpServer.hpp"
#include "InitAddr.hpp"
using namespace std;

void ping(int sockfd, InitAddr& addr) {
    // 读取客户端输入
    char buffer[1024];
    int n = recv(sockfd, buffer, sizeof(buffer) - 1, 0);
    if (n > 0) {
        buffer[n] = '\0';
        // 回发消息
        string echo_string = "Pong";
        n = send(sockfd, echo_string.c_str(), echo_string.size(), 0);
    } else if (0 == n) {
        lg.LogMessage(Debug, "client %s close……", addr.get_Info().c_str());
    } else {
        lg.LogMessage(Error, "read error, error code: %d, error string: %s", errno, strerror(errno));
    }
}

void translate(int sockfd, InitAddr& addr) {
    unordered_map<string, string> dict;
    dict["left"] = "左边";
    dict["right"] = "右边";
    dict["answer"] = "答案";
    dict["apple"] = "苹果";
    dict["address"] = "地址";
    dict["string"] = "字符串";
    dict["array"] = "数组";
    dict["number"] = "数字";

    while (true) {
        // 读取客户端输入
        char buffer[1024];
        int n = recv(sockfd, buffer, sizeof(buffer) - 1, 0);
        if (n > 0) {
            buffer[n] = '\0';
            // 回发消息
            string word = buffer;
            string echo_string;
            if (dict.count(word)) {
                echo_string = dict[word];
            } else {
                echo_string = "unknow";
            }
            n = send(sockfd, echo_string.c_str(), echo_string.size(), 0);
        } else if (0 == n) {
            lg.LogMessage(Debug, "client %s close……", addr.get_Info().c_str());
            break;
        } else {
            lg.LogMessage(Error, "read error, error code: %d, error string: %s", errno, strerror(errno));
            break;
        }        
    }
}

void conv(string& s) {
    int n = s.size();
    for (int i = 0; i < n; i++) {
        if (s[i] >= 'a' && s[i] <= 'z') {
            s[i] -= 32;
        }
    }
}
void convert(int sockfd, InitAddr& addr) {
    while (true) {
        // 读取客户端输入
        char buffer[1024];
        int n = recv(sockfd, buffer, sizeof(buffer) - 1, 0);
        if (n > 0) {
            buffer[n] = '\0';
            // 回发消息
            string word = buffer;
            conv(word);
            string echo_string = word;
            n = send(sockfd, echo_string.c_str(), echo_string.size(), 0);
        } else if (0 == n) {
            lg.LogMessage(Debug, "client %s close……", addr.get_Info().c_str());
            break;
        } else {
            lg.LogMessage(Error, "read error, error code: %d, error string: %s", errno, strerror(errno));
            break;
        }        
    }
}

void Default(int sockfd, InitAddr& addr) {
    while (true) {
        // 读取客户端输入
        char buffer[1024];
        int n = recv(sockfd, buffer, sizeof(buffer) - 1, 0);
        if (n > 0) {
            buffer[n] = '\0';
            // 回发消息
            string word = buffer;
            string echo_string = "I get you: [" + word + "]";
            n = send(sockfd, echo_string.c_str(), echo_string.size(), 0);
        } else if (0 == n) {
            lg.LogMessage(Debug, "client %s close……", addr.get_Info().c_str());
            break;
        } else {
            lg.LogMessage(Error, "read error, error code: %d, error string: %s", errno, strerror(errno));
            break;
        }        
    }  
}


int main(int argc, char* args[]) {
    if (argc != 2) {
        cout << "Usage:\n\r" << args[0] << " local_port\n" << endl;
        return 0; 
    }
    uint16_t local_port = stoi(args[1]);
    // 创建服务器
    unique_ptr<TcpServer> ts = unique_ptr<TcpServer>(new TcpServer(local_port));
    ts->Init();
    ts->Register("ping", ping);
    ts->Register("translate", translate);
    ts->Register("convert", convert);
    ts->Register("default", Default);
    ts->Start();

    return 0;
}   